package com.golxen.framework.timers;


import com.golxen.utils.LogUtil;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.LongSupplier;

public class TimerPlugin {

    static TimerPlugin instance = null;

    public static final int ALL_SYNC_TIME = 300000; // 5min全同步

    public static void start() {
        if (instance != null) {
            return;
        }

        instance = new TimerPlugin();

        for (TimerStartor startor : startors) {
            startor.start();
        }
    }

    public static void stop() {
        if (!active()) {
            return;
        }
        for (TimerStartor timerStartor : startors) {
            timerStartor.stop();
        }
        startors.clear();
    }

    public static boolean active() {
        return instance != null;
    }

    public static void registTimer(Class<?> cls, LongSupplier initialDelay, long delay, TimeUnit unit) {
    	TimerStartor timerStartor = new TimerStartor(cls, initialDelay, delay, unit);

        if (startors.contains(timerStartor)) {
            LogUtil.COMMON.warn("[TimerPlugin] regist timer repeated {}", cls.getName());
            return;
        }

        LogUtil.COMMON.info("[TimerPlugin] regist timer {}", cls.getName());
        startors.add(timerStartor);
    }
    
    public static void registTimer(Class<?> cls, long initialDelay, long delay, TimeUnit unit) {
        TimerStartor timerStartor = new TimerStartor(cls, initialDelay, delay, unit);

        if (startors.contains(timerStartor)) {
            LogUtil.COMMON.warn("[TimerPlugin] regist timer repeated {}", cls.getName());
            return;
        }

        LogUtil.COMMON.debug("[TimerPlugin] regist timer {}", cls.getName());
        startors.add(timerStartor);
    }


    //多线程执行定时当前任务，创建多个同类型定时任务，repeated区分(线程数，比如有16个线程那就创建16个这个定时器类型，有各自的idx)，
    // 执行时用repeated取余判断是否等于这个定时器idx然后继续执行 不然就等下一个定时器的idx判断（MgrDataSaveTask）
    public static void registTimer(Class<?> cls, long initialDelay, long delay, TimeUnit unit, int repeated) {
        for (int i = 0; i < repeated; i++) {
            TimerStartor timerStartor = new TimerStartor(cls, initialDelay, delay, unit, i);
            if (startors.contains(timerStartor)) {
                LogUtil.COMMON.warn("[TimerPlugin] regist timer repeated {}", cls.getName());
                return;
            }
            startors.add(timerStartor);
        }
        LogUtil.COMMON.debug("[TimerPlugin] regist timer {}", cls.getName());
    }

    //距离次日0点的时间
    public static int getNextResetTaskExecuteTime() {
        LocalDateTime currentTime = LocalDateTime.now();
        LocalDateTime nextExecute = currentTime.plusDays(1);
        nextExecute = LocalDateTime.of(nextExecute.getYear(), nextExecute.getMonth(), nextExecute.getDayOfMonth(), 0, 0,
                0, 0);
        return (int) (Duration.between(currentTime, nextExecute).getSeconds());
    }

    static List<TimerStartor> startors = new ArrayList<TimerStartor>();

}
